pacman::p_load("lavaan","tidyverse","kableExtra","riclpmr", "dplyr")

options(knitr.kable.NA = '')

#load data created using "liss_create_data.R"----------
load("Data/liss.RData")

## Figure 2 - set models ---------

CLPM_u <- '
  # Estimate the lagged effects between the observed variables.
  x2 + y2 ~ x1 + y1 + variable
  x3 + y3 ~ x2 + y2 + variable
  x3 ~~ x3
  x2 ~~ x2
  x1 ~~ x1
  y3 ~~ y3
  y2 ~~ y2
  y1 ~~ y1
  y3 ~~ x3
  y2 ~~ x2
  y1 ~~ x1
  y1 ~ variable
'

CLPM_c <- '
  # Estimate the lagged effects between the observed variables.
  x2 + y2 ~ a*x1 + b*y1 + p*variable
  x3 + y3 ~ a*x2 + b*y2 + p*variable
  x3 ~~ x3
  x2 ~~ x2
  x1 ~~ x1
  y3 ~~ y3
  y2 ~~ y2
  y1 ~~ y1
  y3 ~~ x3
  y2 ~~ x2
  y1 ~~ x1
  y1 ~ variable

'

## Figure 2 - Euthanasia -----------------------------------------------------------------
xvars <- colnames(long)[grep("euth", x = colnames(long))][c(1,2,3)]
yvars <- colnames(long)[grep("p", x = colnames(long))][c(1,2,3)]
long %>% dplyr::select(xvars, yvars,nomem_encr,starts_with("education"), starts_with("income"), gender,age,variable) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

euth_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F,cluster="nomem_encr")
euth_clpm2 <- sem(CLPM_c, data = (gss_short),missing="ML",fixed.x=F,cluster="nomem_encr")

## Figure 2 - EU -----------------------------------------------------------------
xvars <- colnames(long)[grep("eu",  x = colnames(long))][c(2,4,6)]
yvars <- colnames(long)[grep("p", x = colnames(long))][c(1,2,3)]
long %>% dplyr::select(xvars, yvars,nomem_encr,starts_with("education"), starts_with("income"), gender,age,variable) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

#wave 2-3: as discussed in main text
summary(lm(x3 ~ y2, data=gss_short))
summary(lm(x2 ~ y1, data=gss_short))

eu_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F,cluster="nomem_encr")
eu_clpm2 <- sem(CLPM_c, data = (gss_short),missing="ML",fixed.x=F,cluster="nomem_encr")

## Figure 2 - Immigration----------------
xvars <- colnames(long)[grep("immi", x = colnames(long))][c(1,2,3)]
yvars <- colnames(long)[grep("p", x = colnames(long))][c(1,2,3)]
long %>% dplyr::select(xvars, yvars,nomem_encr,starts_with("education"), starts_with("income"),gender,age,variable) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

#cross lagged
immi_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F,cluster="nomem_encr")
immi_clpm2 <- sem(CLPM_c, data = (gss_short),missing="ML",fixed.x=F,cluster="nomem_encr")

## Figure 2 - ideology -----------------------------------------------------------------
xvars <- colnames(long)[grep("libcon", x = colnames(long))][c(1,2,3)]
yvars <- colnames(long)[grep("p", x = colnames(long))][c(1,2,3)]
long %>% dplyr::select(xvars, yvars,nomem_encr,starts_with("education"), starts_with("income"),gender,age,variable) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

libcon_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F,cluster="nomem_encr")
libcon_clpm2 <- sem(CLPM_c, data = (gss_short),missing="ML",fixed.x=F,cluster="nomem_encr")

## Figure 2 - income differences -----------------------------------------------------------------
xvars <- colnames(long)[grep("incomediff", x = colnames(long))][c(1,2,3)]
yvars <- colnames(long)[grep("p", x = colnames(long))][c(1,2,3)]
long %>% dplyr::select(xvars, yvars,nomem_encr,starts_with("education"), starts_with("income"),gender,age,variable) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

income_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F,cluster="nomem_encr")
incomediff_clpm2 <- sem(CLPM_c, data = (gss_short),missing="ML",fixed.x=F,cluster="nomem_encr")

## Figure 2 - build figure  ---------------------------------------------------------------
auth_results <-
  gtools::smartbind(
    parameterestimates(libcon_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(euth_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(income_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(immi_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(eu_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")))

auth_results$lhs <- c("DV: Policy Preference","DV: Personality")
auth_results$rhs <- c("IV: Personality","IV: Policy Preference")
auth_results$label <- c(rep("Ideology",4),rep("Euthanasia",4),rep("Income Differences",4),rep("Immigration",4),rep("EU",4))
auth_results$lhs <- factor(auth_results$lhs,levels=c("DV: Policy Preference","DV: Personality"))
auth_results$wave <- c(rep("Wave 2 DV~Wave 1 IV",2),rep("Wave 3 DV~Wave 2 IV",2))
ggplot(auth_results,aes(x=label,y=est,shape=wave))+geom_pointrange(aes(ymin=ci.lower,ymax=ci.upper),position=position_dodge(width=.5))+facet_wrap(lhs~rhs)+geom_hline(yintercept = 0,lty=2)+theme_bw(base_size=15)+labs(shape="Model")+ylab("Standardized Coefficient")+theme(axis.text.x = element_text(angle = 45,hjust=1))+xlab("Policy Variable") + theme(legend.position="bottom")
ggsave("Results/xlaggedplotliss.pdf",width=8,height=6)


## SI 1.5 - Table with Figures fit measures-------------------------
fitstats <- rbind(data.frame(anova(eu_clpm,eu_clpm2)),
                  data.frame(anova(immi_clpm,immi_clpm2)),
                  data.frame(anova(euth_clpm,euth_clpm2)),
                  data.frame(anova(libcon_clpm,libcon_clpm2)),
                  data.frame(anova(income_clpm,incomediff_clpm2)))

fitstats$constrained <- c("Unconstrained","Constrained")
fitstats <- fitstats %>% relocate(constrained)

kbl(fitstats,booktabs = T, digits=2, caption="Fit Statistics for Unconstrained versus Constrained Models", format = "latex", escape = F,row.names = F) %>%
  kable_classic(full_width = F) %>%
  kableExtra::group_rows(start_row = 1,end_row = 2,group_label = "EU") %>%
  kableExtra::group_rows(start_row = 3,end_row = 4,group_label = "Immigration") %>%
  kableExtra::group_rows(start_row = 5,end_row = 6,group_label = "Euthanasia") %>%
  kableExtra::group_rows(start_row = 7,end_row = 8,group_label = "Ideology") %>%
  kableExtra::group_rows(start_row = 9,end_row = 10,group_label = "Income Differences") -> fitstats

fitstats %>% save_kable("Results/fitstatsliss.tex")


## SI 1.4 - Results belonging to Figure 2 for left-right ---------------------------------------------------------------
parameterEstimates(libcon_clpm) %>% filter((str_detect(lhs, "^x") & str_detect(rhs, "^y")) | (str_detect(lhs, "^y") & str_detect(rhs, "^x")) ) %>% dplyr::select(c(1,3:5,7)) -> libcon_param
libcon_param$pvalue <- libcon_param$pvalue/2
fitindicies <-
  rbind(c(
    "RMSEA",
    round(fitmeasures(libcon_clpm)[names(fitmeasures(libcon_clpm)) == "rmsea"], 2),
    paste0("CI:",round(fitmeasures(libcon_clpm)[names(fitmeasures(libcon_clpm)) ==
                                                  "rmsea.ci.lower"], 2),"-",
           round(fitmeasures(libcon_clpm)[names(fitmeasures(libcon_clpm)) ==
                                            "rmsea.ci.upper"], 2)),NA, NA
  ),
  c("CFI", round(fitmeasures(libcon_clpm)[names(fitmeasures(libcon_clpm)) ==
                                            "cfi"], 2), rep(NA, 3)),
  c("SRMR", round(fitmeasures(libcon_clpm)[names(fitmeasures(libcon_clpm)) ==
                                             "srmr"], 2), rep(NA, 3)))
colnames(fitindicies) <- colnames(libcon_param)
libcon_tab <- rbind(libcon_param[1:4,],fitindicies)
libcon_tab[1:4,3:5] <- mapply(function(x)as.character(formatC(round(as.numeric(x),2),2,format="f")), libcon_tab[1:4,3:5])

libcon_tab$lhs <- c("Politics", "Personality","Politics","Personality","RMSEA","CFI","SRMR")
libcon_tab$rhs <- c("$Personality_{t-1}$","$Politics_{t-1}$","$Personality_{t-1}$","$Politics_{t-1}$",libcon_tab$rhs[5:7])
colnames(libcon_tab) <- c("Outcome Measure","Predictor","$\\beta$","SE","p-value")
kbl(libcon_tab,booktabs = T, digits=2, caption="Cross-lagged coefficients between Left-Right Ideological Self-Placement and Closed Personality", format = "latex", escape=F) %>%
  kable_classic(full_width = F) %>%
  kableExtra::group_rows(start_row = 5,end_row = 7,group_label = "Fit Indices") %>%
  kableExtra::group_rows(start_row = 1,end_row = 2,group_label = "Wave 2") %>%
  kableExtra::group_rows(start_row = 3,end_row = 4,group_label = "Wave 3") %>% kable_styling(latex_options = c( "hold_position"))->  libcon_tab
libcon_tab %>% save_kable("Results/lisslibcon.tex")

## SI 1.4 - Results belonging to Figure 2 for Euthanasia ---------------------------------------------------------------
parameterEstimates(euth_clpm) %>% filter((str_detect(lhs, "^x") & str_detect(rhs, "^y")) | (str_detect(lhs, "^y") & str_detect(rhs, "^x")) ) %>% dplyr::select(c(1,3:5,7)) -> euth_param
euth_param$pvalue <- euth_param$pvalue/2
fitindicies <-
  rbind(c(
    "RMSEA",
    round(fitmeasures(euth_clpm)[names(fitmeasures(euth_clpm)) == "rmsea"], 2),
    paste0("CI:",round(fitmeasures(euth_clpm)[names(fitmeasures(euth_clpm)) ==
                                                "rmsea.ci.lower"], 2),"-",
           round(fitmeasures(euth_clpm)[names(fitmeasures(euth_clpm)) ==
                                          "rmsea.ci.upper"], 2)),NA, NA
  ),
  c("CFI", round(fitmeasures(euth_clpm)[names(fitmeasures(euth_clpm)) ==
                                          "cfi"], 2), rep(NA, 3)),
  c("SRMR", round(fitmeasures(euth_clpm)[names(fitmeasures(euth_clpm)) ==
                                           "srmr"], 2), rep(NA, 3)))
colnames(fitindicies) <- colnames(euth_param)
euth_tab <- rbind(euth_param[1:4,],fitindicies)
euth_tab[1:4,3:5] <- mapply(function(x)as.character(formatC(round(as.numeric(x),2),2,format="f")), euth_tab[1:4,3:5])

euth_tab$lhs <-  c("Politics", "Personality","Politics","Personality","RMSEA","CFI","SRMR")
euth_tab$rhs <- c("$Personality_{t-1}$","$Politics_{t-1}$","$Personality_{t-1}$","$Politics_{t-1}$",euth_tab$rhs[5:7])
colnames(euth_tab) <- c("Outcome Measure","Predictor","$\\beta$","SE","p-value")
kbl(euth_tab,booktabs = T, digits=2, caption="Cross-lagged coefficients between Euthanasia Attitudes and Closed Personality", format = "latex", escape=F) %>%
  kable_classic(full_width = F) %>%
  kableExtra::group_rows(start_row = 5,end_row = 7,group_label = "Fit Indices") %>%
  kableExtra::group_rows(start_row = 1,end_row = 2,group_label = "Wave 2") %>%
  kableExtra::group_rows(start_row = 3,end_row = 4,group_label = "Wave 3") %>%  kable_styling(latex_options = c( "hold_position"))->  euth_tab
euth_tab %>% save_kable("Results/lisseuth.tex")

## SI 1.4 - Results belonging to Figure 2 for For EU ---------------------------------------------------------------
parameterEstimates(eu_clpm) %>% filter((str_detect(lhs, "^x") & str_detect(rhs, "^y")) | (str_detect(lhs, "^y") & str_detect(rhs, "^x")) ) %>% dplyr::select(c(1,3:5,7)) -> eu_param
eu_param$pvalue <- eu_param$pvalue/2
fitindicies <-
  rbind(c(
    "RMSEA",
    round(fitmeasures(eu_clpm)[names(fitmeasures(eu_clpm)) == "rmsea"], 2),
    paste0("CI:",round(fitmeasures(eu_clpm)[names(fitmeasures(eu_clpm)) ==
                                              "rmsea.ci.lower"], 2),"-",
           round(fitmeasures(eu_clpm)[names(fitmeasures(eu_clpm)) ==
                                        "rmsea.ci.upper"], 2)),NA, NA
  ),
  c("CFI", round(fitmeasures(eu_clpm)[names(fitmeasures(eu_clpm)) ==
                                        "cfi"], 2), rep(NA, 3)),
  c("SRMR", round(fitmeasures(eu_clpm)[names(fitmeasures(eu_clpm)) ==
                                         "srmr"], 2), rep(NA, 3)))
colnames(fitindicies) <- colnames(eu_param)
eu_tab <- rbind(eu_param[1:4,],fitindicies)
eu_tab[1:4,3:5] <- mapply(function(x)as.character(formatC(round(as.numeric(x),2),2,format="f")), eu_tab[1:4,3:5])
eu_tab$lhs <-  c("Politics", "Personality","Politics","Personality","RMSEA","CFI","SRMR")
eu_tab$rhs <- c("$Personality_{t-1}$","$Politics_{t-1}$","$Personality_{t-1}$","$Politics_{t-1}$",eu_tab$rhs[5:7])
colnames(eu_tab) <- c("Outcome Measure","Predictor","$\\beta$","SE","p-value")
kbl(eu_tab,booktabs = T, digits=2, caption="Cross-lagged coefficients between EU Attitudes and Closed Personality", format = "latex", escape=F) %>%
  kable_classic(full_width = F) %>%
  kableExtra::group_rows(start_row = 5,end_row = 7,group_label = "Fit Indices") %>%
  kableExtra::group_rows(start_row = 1,end_row = 2,group_label = "Wave 2") %>%
  kableExtra::group_rows(start_row = 3,end_row = 4,group_label = "Wave 3") %>%  kable_styling(latex_options = c( "hold_position"))->  eu_tab
eu_tab %>% save_kable("Results/lisseu.tex")


## SI 1.4 - Results belonging to Figure 2 for Immigration ---------------------------------------------------------------
parameterEstimates(immi_clpm) %>% filter((str_detect(lhs, "^x") & str_detect(rhs, "^y")) | (str_detect(lhs, "^y") & str_detect(rhs, "^x")) ) %>% dplyr::select(c(1,3:5,7)) -> immi_param
immi_param$pvalue <- immi_param$pvalue/2
fitindicies <-
  rbind(c(
    "RMSEA",
    round(fitmeasures(immi_clpm)[names(fitmeasures(immi_clpm)) == "rmsea"], 2),
    paste0("CI:",round(fitmeasures(immi_clpm)[names(fitmeasures(immi_clpm)) ==
                                                "rmsea.ci.lower"], 2),"-",
           round(fitmeasures(immi_clpm)[names(fitmeasures(immi_clpm)) ==
                                          "rmsea.ci.upper"], 2)),NA, NA
  ),
  c("CFI", round(fitmeasures(immi_clpm)[names(fitmeasures(immi_clpm)) ==
                                          "cfi"], 2), rep(NA, 3)),
  c("SRMR", round(fitmeasures(immi_clpm)[names(fitmeasures(immi_clpm)) ==
                                           "srmr"], 2), rep(NA, 3)))
colnames(fitindicies) <- colnames(immi_param)
immi_tab <- rbind(immi_param[1:4,],fitindicies)
immi_tab[1:4,3:5] <- mapply(function(x)as.character(formatC(round(as.numeric(x),2),2,format="f")), immi_tab[1:4,3:5])

immi_tab$lhs <-  c("Politics", "Personality","Politics","Personality","RMSEA","CFI","SRMR")
immi_tab$rhs <- c("$Personality_{t-1}$","$Politics_{t-1}$","$Personality_{t-1}$","$Politics_{t-1}$",immi_tab$rhs[5:7])
colnames(immi_tab) <- c("Outcome Measure","Predictor","$\\beta$","SE","p-value")
kbl(immi_tab,booktabs = T, digits=2, caption="Cross-lagged coefficients between Immigration Attitudes and Closed Personality", format = "latex", escape=F) %>%
  kable_classic(full_width = F) %>%
  kableExtra::group_rows(start_row = 5,end_row = 7,group_label = "Fit Indices") %>%
  kableExtra::group_rows(start_row = 1,end_row = 2,group_label = "Wave 2") %>%
  kableExtra::group_rows(start_row = 3,end_row = 4,group_label = "Wave 3") %>%  kable_styling(latex_options = c( "hold_position"))->  immi_tab
immi_tab %>% save_kable("Results/lissimmi.tex")

## SI 1.4 - Results belonging to Figure 2 for income differences ---------------------------------------------------------------
parameterEstimates(income_clpm) %>% filter((str_detect(lhs, "^x") & str_detect(rhs, "^y")) | (str_detect(lhs, "^y") & str_detect(rhs, "^x")) ) %>% dplyr::select(c(1,3:5,7)) -> income_param
income_param$pvalue <- income_param$pvalue/2
fitindicies <-
  rbind(c(
    "RMSEA",
    round(fitmeasures(income_clpm)[names(fitmeasures(income_clpm)) == "rmsea"], 2),
    paste0("CI:",round(fitmeasures(income_clpm)[names(fitmeasures(income_clpm)) ==
                                                  "rmsea.ci.lower"], 2),"-",
           round(fitmeasures(income_clpm)[names(fitmeasures(income_clpm)) ==
                                            "rmsea.ci.upper"], 2)),NA, NA
  ),
  c("CFI", round(fitmeasures(income_clpm)[names(fitmeasures(income_clpm)) ==
                                            "cfi"], 2), rep(NA, 3)),
  c("SRMR", round(fitmeasures(income_clpm)[names(fitmeasures(income_clpm)) ==
                                             "srmr"], 2), rep(NA, 3)))
colnames(fitindicies) <- colnames(income_param)
income_tab <- rbind(income_param[1:4,],fitindicies)
income_tab[1:4,3:5] <- mapply(function(x)as.character(formatC(round(as.numeric(x),2),2,format="f")), income_tab[1:4,3:5])

income_tab$lhs <-  c("Politics", "Personality","Politics","Personality","RMSEA","CFI","SRMR")
income_tab$rhs <- c("$Personality_{t-1}$","$Politics_{t-1}$","$Personality_{t-1}$","$Politics_{t-1}$",income_tab$rhs[5:7])
colnames(income_tab) <- c("Outcome Measure","Predictor","$\\beta$","SE","p-value")
kbl(income_tab,booktabs = T, digits=2, caption="Cross-lagged coefficients between Income Redistribution Attitudes and Closed Personality", format = "latex", escape=F) %>%
  kable_classic(full_width = F) %>%
  kableExtra::group_rows(start_row = 5,end_row = 7,group_label = "Fit Indices") %>%
  kableExtra::group_rows(start_row = 1,end_row = 2,group_label = "Wave 2") %>%
  kableExtra::group_rows(start_row = 3,end_row = 4,group_label = "Wave 3") %>%  kable_styling(latex_options = c( "hold_position"))->  income_tab
income_tab %>% save_kable("Results/lissincome.tex")

## SI 1.6 - RICPLM euthanasia model--------------
riclpmtext <- riclpm_text(var_groups = list(x=c("x1","x2","x3"),y=c("y1","y2","y3")))
xvars <- colnames(long)[grep("euth", x = colnames(long))][c(1,2,3)]
yvars <- colnames(long)[grep("p", x = colnames(long))][c(1,2,3)]
long %>% dplyr::select(xvars, yvars,nomem_encr, variable) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)
gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)
euth_tab <- riclpmr::lavriclpm(riclpmtext,data = gss_short,missing="ML")

## SI 1.6 - RICPLM EU model----------------
xvars <- colnames(long)[grep("eu", x = colnames(long))][c(2,4,6)]
yvars <- colnames(long)[grep("p", x = colnames(long))][c(1,2,3)]
long %>% dplyr::select(xvars, yvars,nomem_encr, variable) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)
gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)
eu_tab <- riclpmr::lavriclpm(riclpmtext,data = gss_short,missing="ML")

## SI 1.6 - RICPLM immigration model ------------------
xvars <- colnames(long)[grep("immi", x = colnames(long))][c(1,2,3)]
yvars <- colnames(long)[grep("p", x = colnames(long))][c(1,2,3)]
long %>% dplyr::select(xvars, yvars,nomem_encr, variable) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)
gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)
immi_tab <- riclpmr::lavriclpm(riclpmtext,data = gss_short,missing="ML" )

## SI 1.6 - RICPLM left-right ideology model-----------------
xvars <- colnames(long)[grep("libcon", x = colnames(long))][c(1,2,3)]
yvars <- colnames(long)[grep("p", x = colnames(long))][c(1,2,3)]
long %>% dplyr::select(xvars, yvars,nomem_encr,starts_with("education"), starts_with("income"),gender,age,variable) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)
gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)
ideology_tab <- riclpmr::lavriclpm(riclpmtext,data = gss_short,missing="ML" )

## SI 1.6 - RICPLM income differences model --------------
xvars <- colnames(long)[grep("incomediff", x = colnames(long))][c(1,2,3)]
yvars <- colnames(long)[grep("p", x = colnames(long))][c(1,2,3)]
long %>% dplyr::select(xvars, yvars,nomem_encr,starts_with("education"), starts_with("income"),gender,age,variable) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)
ideology_tab <- riclpmr::lavriclpm(riclpmtext,data = gss_short,missing="ML" )

## SI 1.6 - RICPLM prepare data for table----------
parameterEstimates(euth_tab) %>% filter(label %in% c("x_y","y_x"))%>%  slice(1,3) -> euth_tab
euth_tab$pvalue <- euth_tab$pvalue/2
parameterEstimates(ideology_tab) %>% filter(label %in% c("x_y","y_x"))%>%  slice(1,3) -> ideology_tab
ideology_tab$pvalue <- ideology_tab$pvalue/2
parameterEstimates(immi_tab) %>% filter(label %in% c("x_y","y_x"))%>%  slice(1,3) -> immi_tab
immi_tab$pvalue <- immi_tab$pvalue/2
parameterEstimates(eu_tab) %>% filter(label %in% c("x_y","y_x"))%>%  slice(1,3) -> eu_tab
eu_tab$pvalue <- eu_tab$pvalue/2

rbind(euth_tab,ideology_tab,immi_tab,eu_tab)
riclpmliss <- rbind(euth_tab,ideology_tab,immi_tab,eu_tab)
riclpmliss$dv <- c("Politics","Personality")
riclpmliss$iv <- c("$Personality_{t-1}$", "$Politics_{t-1}$")
riclpmliss %>% dplyr::select(dv,iv,est,se,pvalue)
riclpmliss %>% dplyr::select(dv,iv,est,se,pvalue) -> riclpmliss

kbl(riclpmliss,booktabs = T, digits=2, caption="Cross-lagged coefficients from RI-CLPM models in LISS data", format = "latex",escape = F) %>% kable_styling(latex_options = c( "hold_position")) %>%
  kable_classic(full_width = F) %>%
  kableExtra::group_rows(start_row = 1,end_row = 2,group_label = "Euthanasia Attitudes") %>%
  kableExtra::group_rows(start_row = 3,end_row = 4,group_label = "Ideology Attitudes") %>%
  kableExtra::group_rows(start_row = 5,end_row = 6,group_label = "Immigrant Attitudes") %>%
  kableExtra::group_rows(start_row = 7,end_row = 8,group_label = "EU Attitudes") %>% save_kable("Results/LISSRICLPM.tex")

## SI 1.7 - Set model-----------
CLPM_u <- '
  # Estimate the lagged effects between the observed variables.
  x2 + y2 ~ x1 + y1
  x3 + y3 ~ x2 + y2
  x3 ~~ x3
  x2 ~~ x2
  x1 ~~ x1
  y3 ~~ y3
  y2 ~~ y2
  y1 ~~ y1
  y3 ~~ x3
  y2 ~~ x2
  y1 ~~ x1
'
yvars <- colnames(all)[grep("open", x = colnames(all))][c(1,2,3)]
## SI 1.7 - Openness: Euthansia ---------------
xvars <- colnames(all)[grep("euth", x = colnames(all))]
all %>% dplyr::select(xvars, yvars) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

euth_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F)

## SI 1.7 - Openness: Europe ---------------
xvars <- colnames(all)[grep("eu", x = colnames(all))][c(2,4,6)]
all %>% dplyr::select(xvars, yvars) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

eu_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F)

## SI 1.7 - Openness: Immigration ----------
xvars <- colnames(all)[grep("immi", x = colnames(all))]
all %>% dplyr::select(xvars, yvars) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

immi_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F)

## SI 1.7 - Openness: ideology -----------------------------------------------------------------
xvars <- colnames(all)[grep("libcon", x = colnames(all))]
all %>% dplyr::select(xvars, yvars) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

libcon_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F)

## SI 1.7 - Openness: income differences -----------------------------------------------------------------
xvars <- colnames(all)[grep("incomediff", x = colnames(all))]
all %>% dplyr::select(xvars, yvars) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

income_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F)

## SI 1.7 - Openness: build plot----------------

auth_results <-
  gtools::smartbind(
    parameterestimates(libcon_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(euth_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(income_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(immi_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(eu_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")))

auth_results$lhs <- c("DV: Policy Preference","DV: Personality")
auth_results$rhs <- c("IV: Personality","IV: Policy Preference")
auth_results$label <- c(rep("Ideology",4),rep("Euthanasia",4),rep("Income Differences",4),rep("Immigration",4),rep("EU",4))
auth_results$lhs <- factor(auth_results$lhs,levels=c("DV: Policy Preference","DV: Personality"))
auth_results$wave <- c(rep("Wave 2 DV~Wave 1 IV",2),rep("Wave 3 DV~Wave 2 IV",2))
ggplot(auth_results,aes(x=label,y=est,shape=wave))+geom_pointrange(aes(ymin=ci.lower,ymax=ci.upper),position=position_dodge(width=.5))+facet_wrap(lhs~rhs)+geom_hline(yintercept = 0,lty=2)+theme_bw(base_size=15)+labs(shape="Model")+ylab("Standardized Coefficient")+theme(axis.text.x = element_text(angle = 45,hjust=1))+xlab("Policy Variable") + theme(legend.position="bottom")
ggsave("Results/xlaggedplotliss_openness.pdf",width=8,height=6)

## SI 1.7 - Conscientiousness: set model -----------
yvars <- colnames(all)[grep("con", x = colnames(all))][c(4,5,6)]

## SI 1.7 - Conscientiousness: Euthansia ---------------
xvars <- colnames(all)[grep("euth", x = colnames(all))][c(1,2,3)]
all %>% dplyr::select(xvars, yvars) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)
euth_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F)

## SI 1.7 - Conscientiousness: Europe ---------------
xvars <- colnames(all)[grep("eu", x = colnames(all))][c(2,4,6)]
all %>% dplyr::select(xvars, yvars) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)
eu_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F)

## SI 1.7 - Conscientiousness: Immigration ----------
xvars <- colnames(all)[grep("immi", x = colnames(all))]
all %>% dplyr::select(xvars, yvars) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)
immi_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F)

## SI 1.7 - Conscientiousness: ideology -----------------------------------------------------------------
xvars <- colnames(all)[grep("libcon", x = colnames(all))]
all %>% dplyr::select(xvars, yvars) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

libcon_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F)

## SI 1.7 - Conscientiousness: income differences -----------------------------------------------------------------
xvars <- colnames(all)[grep("incomediff", x = colnames(all))]
all %>% dplyr::select(xvars, yvars) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)
income_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F)

## SI 1.7 - Conscientiousness: build plot----------------

auth_results <-
  gtools::smartbind(
    parameterestimates(libcon_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(euth_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(income_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(immi_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(eu_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")))

auth_results$lhs <- c("DV: Policy Preference","DV: Personality")
auth_results$rhs <- c("IV: Personality","IV: Policy Preference")
auth_results$label <- c(rep("Ideology",4),rep("Euthanasia",4),rep("Income Differences",4),rep("Immigration",4),rep("EU",4))
auth_results$lhs <- factor(auth_results$lhs,levels=c("DV: Policy Preference","DV: Personality"))
auth_results$wave <- c(rep("Wave 2 DV~Wave 1 IV",2),rep("Wave 3 DV~Wave 2 IV",2))
ggplot(auth_results,aes(x=label,y=est,shape=wave))+geom_pointrange(aes(ymin=ci.lower,ymax=ci.upper),position=position_dodge(width=.5))+facet_wrap(lhs~rhs)+geom_hline(yintercept = 0,lty=2)+theme_bw(base_size=15)+labs(shape="Model")+ylab("Standardized Coefficient")+theme(axis.text.x = element_text(angle = 45,hjust=1))+xlab("Policy Variable") + theme(legend.position="bottom")
ggsave("Results/xlaggedplotliss_conscientiousness.pdf",width=8,height=6)


## SI 1.8 - set model
CLPM_u <- '
  # Estimate the lagged effects between the observed variables.
  x2 + y2 ~ x1 + y1
  x3 + y3 ~ x2 + y2
  x3 ~~ x3
  x2 ~~ x2
  x1 ~~ x1
  y3 ~~ y3
  y2 ~~ y2
  y1 ~~ y1
  y3 ~~ x3
  y2 ~~ x2
  y1 ~~ x1
'
yvars <- colnames(all)[grep("neu", x = colnames(all))][c(1,2,3)]

## SI 1.8 - Neuroticism: Euthansia ---------------
xvars <- colnames(all)[grep("euth", x = colnames(all))]
all %>% dplyr::select(xvars, yvars) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

euth_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F)


## SI 1.8 - Neuroticism: Europe ---------------
xvars <- colnames(all)[grep("eu", x = colnames(all))][c(2,4,6)]
all %>% dplyr::select(xvars, yvars) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

eu_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F)

## SI 1.8 - Neuroticism: Immigration ----------
xvars <- colnames(all)[grep("immi", x = colnames(all))]
all %>% dplyr::select(xvars, yvars) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

immi_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F)

## SI 1.8 - Neuroticism: ideology -----------------------------------------------------------------
xvars <- colnames(all)[grep("libcon", x = colnames(all))]
all %>% dplyr::select(xvars, yvars) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

libcon_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F)

## SI 1.8 - Neuroticism: income differences -----------------------------------------------------------------
xvars <- colnames(all)[grep("incomediff", x = colnames(all))]
all %>% dplyr::select(xvars, yvars) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)
income_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F)

## SI 1.8 - Neuroticism: build plot----------------

auth_results <-
  gtools::smartbind(
    parameterestimates(libcon_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(euth_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(income_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(immi_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(eu_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")))

auth_results$lhs <- c("DV: Policy Preference","DV: Personality")
auth_results$rhs <- c("IV: Personality","IV: Policy Preference")
auth_results$label <- c(rep("Ideology",4),rep("Euthanasia",4),rep("Income Differences",4),rep("Immigration",4),rep("EU",4))
auth_results$lhs <- factor(auth_results$lhs,levels=c("DV: Policy Preference","DV: Personality"))
auth_results$wave <- c(rep("Wave 2 DV~Wave 1 IV",2),rep("Wave 3 DV~Wave 2 IV",2))
ggplot(auth_results,aes(x=label,y=est,shape=wave))+geom_pointrange(aes(ymin=ci.lower,ymax=ci.upper),position=position_dodge(width=.5))+facet_wrap(lhs~rhs)+geom_hline(yintercept = 0,lty=2)+theme_bw(base_size=15)+labs(shape="Model")+ylab("Standardized Coefficient")+theme(axis.text.x = element_text(angle = 45,hjust=1))+xlab("Policy Variable") + theme(legend.position="bottom")
ggsave("Results/xlaggedplotliss_neuroticism.pdf",width=8,height=6)

## SI 1.8 - Extraversion set model ------------
yvars <- colnames(all)[grep("ext", x = colnames(all))][c(1,2,3)]

## SI 1.8 - Extraversion: Euthansia ---------------
xvars <- colnames(all)[grep("euth", x = colnames(all))]
all %>% dplyr::select(xvars, yvars) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

euth_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F)


## SI 1.8 - Extraversion: Europe ---------------
xvars <- colnames(all)[grep("eu", x = colnames(all))][c(2,4,6)]
all %>% dplyr::select(xvars, yvars) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

eu_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F)

## SI 1.8 - Extraversion: Immigration ----------
xvars <- colnames(all)[grep("immi", x = colnames(all))]
all %>% dplyr::select(xvars, yvars) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

immi_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F)

## SI 1.8 - Extraversion: ideology -----------------------------------------------------------------
xvars <- colnames(all)[grep("libcon", x = colnames(all))]
all %>% dplyr::select(xvars, yvars) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

libcon_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F)

## SI 1.8 - Extraversion: income differences -----------------------------------------------------------------
xvars <- colnames(all)[grep("incomediff", x = colnames(all))]
all %>% dplyr::select(xvars, yvars) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

income_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F)

## SI 1.8 - Extraversion: build plot----------------

auth_results <-
  gtools::smartbind(
    parameterestimates(libcon_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(euth_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(income_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(immi_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(eu_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")))

auth_results$lhs <- c("DV: Policy Preference","DV: Personality")
auth_results$rhs <- c("IV: Personality","IV: Policy Preference")
auth_results$label <- c(rep("Ideology",4),rep("Euthanasia",4),rep("Income Differences",4),rep("Immigration",4),rep("EU",4))
auth_results$lhs <- factor(auth_results$lhs,levels=c("DV: Policy Preference","DV: Personality"))
auth_results$wave <- c(rep("Wave 2 DV~Wave 1 IV",2),rep("Wave 3 DV~Wave 2 IV",2))
ggplot(auth_results,aes(x=label,y=est,shape=wave))+geom_pointrange(aes(ymin=ci.lower,ymax=ci.upper),position=position_dodge(width=.5))+facet_wrap(lhs~rhs)+geom_hline(yintercept = 0,lty=2)+theme_bw(base_size=15)+labs(shape="Model")+ylab("Standardized Coefficient")+theme(axis.text.x = element_text(angle = 45,hjust=1))+xlab("Policy Variable") + theme(legend.position="bottom")
ggsave("Results/xlaggedplotliss_extraversion.pdf",width=8,height=6)


## SI 1.8 - Agreeableness: set model ----------
yvars <- colnames(all)[grep("agre", x = colnames(all))]

## SI 1.8 - Agreeableness: Euthansia ---------------
xvars <- colnames(all)[grep("euth", x = colnames(all))]
all %>% dplyr::select(xvars, yvars) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

euth_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F)


## SI 1.8 - Agreeableness: Europe ---------------
xvars <- colnames(all)[grep("eu", x = colnames(all))][c(2,4,6)]
all %>% dplyr::select(xvars, yvars) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

eu_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F)

## SI 1.8 - Agreeableness: Immigration ----------
xvars <- colnames(all)[grep("immi", x = colnames(all))]
all %>% dplyr::select(xvars, yvars) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

immi_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F)

## SI 1.8 - Agreeableness: ideology -----------------------------------------------------------------
xvars <- colnames(all)[grep("libcon", x = colnames(all))]
all %>% dplyr::select(xvars, yvars) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

libcon_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F)

## SI 1.8 - Agreeableness: income differences -----------------------------------------------------------------
xvars <- colnames(all)[grep("incomediff", x = colnames(all))]
all %>% dplyr::select(xvars, yvars) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

income_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F)

## SI 1.8 - Agreeableness: build plot----------------
auth_results <-
  gtools::smartbind(
    parameterestimates(libcon_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(euth_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(income_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(immi_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(eu_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")))

auth_results$lhs <- c("DV: Policy Preference","DV: Personality")
auth_results$rhs <- c("IV: Personality","IV: Policy Preference")
auth_results$label <- c(rep("Ideology",4),rep("Euthanasia",4),rep("Income Differences",4),rep("Immigration",4),rep("EU",4))
auth_results$lhs <- factor(auth_results$lhs,levels=c("DV: Policy Preference","DV: Personality"))
auth_results$wave <- c(rep("Wave 2 DV~Wave 1 IV",2),rep("Wave 3 DV~Wave 2 IV",2))
ggplot(auth_results,aes(x=label,y=est,shape=wave))+geom_pointrange(aes(ymin=ci.lower,ymax=ci.upper),position=position_dodge(width=.5))+facet_wrap(lhs~rhs)+geom_hline(yintercept = 0,lty=2)+theme_bw(base_size=15)+labs(shape="Model")+ylab("Standardized Coefficient")+theme(axis.text.x = element_text(angle = 45,hjust=1))+xlab("Policy Variable") + theme(legend.position="bottom")
ggsave("Results/xlaggedplotliss_agreeableness.pdf",width=8,height=6)


## SI 1.9 - with controls: set model --------
CLPM_u <- '
  # Estimate the lagged effects between the observed variables.
  x2 + y2 ~ x1 + y1 + variable + education_1 + education_2 + education_3 +education_4 + education_5 + education_6 + income4_1 + income4_2 + income4_3 + income4_4 + gender + age
  x3 + y3 ~ x2 + y2 + variable + education_1 + education_2 + education_3 +education_4 + education_5 + education_6 + income4_1 + income4_2 + income4_3 + income4_4 + gender + age
  x3 ~~ x3
  x2 ~~ x2
  x1 ~~ x1
  y3 ~~ y3
  y2 ~~ y2
  y1 ~~ y1
  y3 ~~ x3
  y2 ~~ x2
  y1 ~~ x1
  y1 ~ variable
'

yvars <- colnames(long)[grep("p", x = colnames(long))]

## SI 1.9 - with controls:# Euthanasia -----------------------------------------------------------------
xvars <- colnames(long)[grep("euth", x = colnames(long))]
long %>% dplyr::select(xvars, yvars,nomem_encr,starts_with("education"), starts_with("income"), gender,age,variable) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

euth_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F,cluster="nomem_encr")

## SI 1.9 - with controls: EU -----------------------------------------------------------------
xvars <- colnames(long)[grep("eu", x = colnames(long))][c(2,4,6)]
long %>% dplyr::select(xvars, yvars,nomem_encr,starts_with("education"), starts_with("income"), gender,age,variable) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

eu_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F,cluster="nomem_encr")
## SI 1.9 - with controls: Immigration----------------
xvars <- colnames(long)[grep("immi", x = colnames(long))]
long %>% dplyr::select(xvars, yvars,nomem_encr,starts_with("education"), starts_with("income"),gender,age,variable) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

immi_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F,cluster="nomem_encr")

## SI 1.9 - with controls: ideology -----------------------------------------------------------------
xvars <- colnames(long)[grep("libcon", x = colnames(long))]
long %>% dplyr::select(xvars, yvars,nomem_encr,starts_with("education"), starts_with("income"),gender,age,variable) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)
gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

libcon_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F,cluster="nomem_encr")

## SI 1.9 - with controls: income differences -----------------------------------------------------------------
xvars <- colnames(long)[grep("incomediff", x = colnames(long))]
long %>% dplyr::select(xvars, yvars,nomem_encr,starts_with("education"), starts_with("income"),gender,age,variable) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)
gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

income_clpm <- sem(CLPM_u, data = (gss_short),missing="ML",fixed.x=F,cluster="nomem_encr")

## SI 1.9 - create figure ---------------------------------------------------------------
auth_results <-
  gtools::smartbind(
    parameterestimates(libcon_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(euth_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(income_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(immi_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(eu_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")))

auth_results$lhs <- c("DV: Policy Preference","DV: Personality")
auth_results$rhs <- c("IV: Personality","IV: Policy Preference")
auth_results$label <- c(rep("Ideology",4),rep("Euthanasia",4),rep("Income Differences",4),rep("Immigration",4),rep("EU",4))
auth_results$lhs <- factor(auth_results$lhs,levels=c("DV: Policy Preference","DV: Personality"))
auth_results$wave <- c(rep("Wave 2 DV~Wave 1 IV",2),rep("Wave 3 DV~Wave 2 IV",2))
ggplot(auth_results,aes(x=label,y=est,shape=wave))+geom_pointrange(aes(ymin=ci.lower,ymax=ci.upper),position=position_dodge(width=.5))+facet_wrap(lhs~rhs)+geom_hline(yintercept = 0,lty=2)+theme_bw(base_size=15)+labs(shape="Model")+ylab("Standardized Coefficient")+theme(axis.text.x = element_text(angle = 45,hjust=1))+xlab("Policy Variable") + theme(legend.position="bottom")
ggsave("Results/xlaggedplotliss_withcontrols.pdf",width=8,height=6)


## SI 1.9 - Age: set model
yvars <- colnames(long)[grep("p", x = colnames(long))]

CLPM_u <- '
  # Estimate the lagged effects between the observed variables.
  x2 + y2 ~ x1 + y1
  x3 + y3 ~ x2 + y2
  x3 ~~ x3
  x2 ~~ x2
  x1 ~~ x1
  y3 ~~ y3
  y2 ~~ y2
  y1 ~~ y1
  y3 ~~ x3
  y2 ~~ x2
  y1 ~~ x1
  y1 ~ variable
'

## SI 9.1 - Age: Euthanasia -----------------------------------------------------------------
xvars <- colnames(long)[grep("euth", x = colnames(long))]
long %>% dplyr::select(xvars, yvars, age,nomem_encr, variable) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)
gss_short$age_median<-ifelse(gss_short$age>median(gss_short$age, na.rm=T), 1,0)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

euth_clpm_young <- sem(CLPM_u, data = subset(gss_short, age_median==0),missing="ML",fixed.x=F,cluster="nomem_encr")
euth_clpm_old <- sem(CLPM_u, data = subset(gss_short, age_median==1),missing="ML",fixed.x=F,cluster="nomem_encr")


## SI 9.1 - Age: Europe----------
xvars <- colnames(long)[grep("eu", x = colnames(long))][c(2,4,6)]
long %>% dplyr::select(xvars, yvars, age,nomem_encr, variable) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)
gss_short$age_median<-ifelse(gss_short$age>median(gss_short$age, na.rm=T), 1,0)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

eu_clpm_young <- sem(CLPM_u, data = subset(gss_short, age_median==0),missing="ML",fixed.x=F,cluster="nomem_encr")
eu_clpm_old <- sem(CLPM_u, data = subset(gss_short, age_median==1),missing="ML",fixed.x=F,cluster="nomem_encr")

## SI 9.1 - Age: Immigration----------
xvars <- colnames(long)[grep("immi", x = colnames(long))]
long %>% dplyr::select(xvars, yvars, age,nomem_encr, variable) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)
gss_short$age_median<-ifelse(gss_short$age>median(gss_short$age, na.rm=T), 1,0)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

immi_clpm_young <- sem(CLPM_u, data = subset(gss_short, age_median==0),missing="ML",fixed.x=F,cluster="nomem_encr")
immi_clpm_old <- sem(CLPM_u, data = subset(gss_short, age_median==1),missing="ML",fixed.x=F,cluster="nomem_encr")

## SI 9.1 - Age: ideology -----------------------------------------------------------------
xvars <- colnames(long)[grep("libcon", x = colnames(long))]
long %>% dplyr::select(xvars, yvars, age,nomem_encr, variable) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)
gss_short$age_median<-ifelse(gss_short$age>median(gss_short$age, na.rm=T), 1,0)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

libcon_clpm_young <- sem(CLPM_u, data = subset(gss_short, age_median==0),missing="ML",fixed.x=F,cluster="nomem_encr")
libcon_clpm_old <- sem(CLPM_u, data = subset(gss_short, age_median==1),missing="ML",fixed.x=F,cluster="nomem_encr")

## SI 9.1 - Age: income differences -----------------------------------------------------------------
xvars <- colnames(long)[grep("incomediff", x = colnames(long))]
long %>% dplyr::select(xvars, yvars, age,nomem_encr, variable) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)
gss_short$age_median<-ifelse(gss_short$age>median(gss_short$age, na.rm=T), 1,0)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

income_clpm_young <- sem(CLPM_u, data = subset(gss_short, age_median==0),missing="ML",fixed.x=F,cluster="nomem_encr")
income_clpm_old <- sem(CLPM_u, data = subset(gss_short, age_median==1),missing="ML",fixed.x=F,cluster="nomem_encr")

## SI 9.1 - Age: create figure -------------
auth_results <-
  gtools::smartbind(
    parameterestimates(libcon_clpm_young,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(euth_clpm_young,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(income_clpm_young,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(immi_clpm_young,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(eu_clpm_young,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")))

auth_results$lhs <- c("DV: Policy Preference","DV: Personality")
auth_results$rhs <- c("IV: Personality","IV: Policy Preference")
auth_results$label <- c(rep("Ideology",4),rep("Euthanasia",4),rep("Income Differences",4),rep("Immigration",4),rep("EU",4))
auth_results$age <- "young"
auth_results$wave <- c(rep("Wave 2 DV~Wave 1 IV",2),rep("Wave 3 DV~Wave 2 IV",2))


auth_results_old <-
  gtools::smartbind(
    parameterestimates(libcon_clpm_old,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(euth_clpm_old,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(income_clpm_old,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(immi_clpm_old,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(eu_clpm_old,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")))

auth_results_old$lhs <- c("DV: Policy Preference","DV: Personality")
auth_results_old$rhs <- c("IV: Personality","IV: Policy Preference")
auth_results_old$label <- c(rep("Ideology",4),rep("Euthanasia",4),rep("Income Differences",4),rep("Immigration",4),rep("EU",4))
auth_results_old$age <- "old"

auth_results_old$wave <- c(rep("Wave 2 DV~Wave 1 IV",2),rep("Wave 3 DV~Wave 2 IV",2))
comb_graph<- rbind(auth_results, auth_results_old)
comb_graph$lhs <- factor(auth_results$lhs,levels=c("DV: Policy Preference","DV: Personality"))

ggplot(comb_graph,aes(x=label,y=est,shape=wave, colour=age))+geom_pointrange(aes(ymin=ci.lower,ymax=ci.upper),position=position_dodge(width=.5))+facet_wrap(lhs~rhs)+geom_hline(yintercept = 0,lty=2)+theme_bw(base_size=15)+labs(shape="Model")+ylab("Standardized Coefficient")+theme(axis.text.x = element_text(angle = 45,hjust=1))+xlab("Policy Variable") + theme(legend.position="bottom")
ggsave("Results/xlaggedplotliss_by_age.pdf",width=8,height=6)

## SI 9.2 - Education: set model ------------
yvars <- colnames(long)[grep("p", x = colnames(long))]
## SI 9.2 - Education: Euthanasia -----------------------------------------------------------------
xvars <- colnames(long)[grep("euth", x = colnames(long))]
long %>% dplyr::select(xvars, yvars, education,nomem_encr, variable) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)
gss_short$edu_median<-ifelse(gss_short$education>4, 1,0)
gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

euth_clpm_low <- sem(CLPM_u, data = subset(gss_short, edu_median==0),missing="ML",fixed.x=F,cluster="nomem_encr")
euth_clpm_high <- sem(CLPM_u, data = subset(gss_short, edu_median==1),missing="ML",fixed.x=F,cluster="nomem_encr")

## SI 9.2 - Education: Europe----------
xvars <- colnames(long)[grep("eu", x = colnames(long))][c(2,4,6)]
long %>% dplyr::select(xvars, yvars, education,nomem_encr, variable) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)
gss_short$edu_median<-ifelse(gss_short$education>4, 1,0)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

eu_clpm_low <- sem(CLPM_u, data = subset(gss_short, edu_median==0),missing="ML",fixed.x=F,cluster="nomem_encr")
eu_clpm_high <- sem(CLPM_u, data = subset(gss_short, edu_median==1),missing="ML",fixed.x=F,cluster="nomem_encr")

## SI 9.2 - Education: Immigration----------
xvars <- colnames(long)[grep("immi", x = colnames(long))]
long %>% dplyr::select(xvars, yvars, education,nomem_encr, variable) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)
gss_short$edu_median<-ifelse(gss_short$education>4, 1,0)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

immi_clpm_low <- sem(CLPM_u, data = subset(gss_short, edu_median==0),missing="ML",fixed.x=F,cluster="nomem_encr")
immi_clpm_high <- sem(CLPM_u, data = subset(gss_short, edu_median==1),missing="ML",fixed.x=F,cluster="nomem_encr")

## SI 9.2 - Education: ideology -----------------------------------------------------------------
xvars <- colnames(long)[grep("libcon", x = colnames(long))]
long %>% dplyr::select(xvars, yvars, education,nomem_encr, variable) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)
gss_short$edu_median<-ifelse(gss_short$education>4, 1,0)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

libcon_clpm_low <- sem(CLPM_u, data = subset(gss_short, edu_median==0),missing="ML",fixed.x=F,cluster="nomem_encr")
libcon_clpm_high <- sem(CLPM_u, data = subset(gss_short, edu_median==1),missing="ML",fixed.x=F,cluster="nomem_encr")

## SI 9.2 - Education: income differences -----------------------------------------------------------------
xvars <- colnames(long)[grep("incomediff", x = colnames(long))]
long %>% dplyr::select(xvars, yvars, education,nomem_encr, variable) -> gss_short
names(gss_short)[1:3] <- paste0("x", 1:3)
names(gss_short)[4:6] <- paste0("y", 1:3)
gss_short$edu_median<-ifelse(gss_short$education>4, 1,0)

gss_short$x1<- scale(gss_short$x1)
gss_short$x2<- scale(gss_short$x2)
gss_short$x3<- scale(gss_short$x3)
gss_short$y1<- scale(gss_short$y1)
gss_short$y2<- scale(gss_short$y2)
gss_short$y3<- scale(gss_short$y3)

income_clpm_low <- sem(CLPM_u, data = subset(gss_short, edu_median==0),missing="ML",fixed.x=F,cluster="nomem_encr")
income_clpm_high <- sem(CLPM_u, data = subset(gss_short, edu_median==1),missing="ML",fixed.x=F,cluster="nomem_encr")

## SI 9.2 - Education: figure -------------
auth_results <-
  gtools::smartbind(
    parameterestimates(libcon_clpm_low,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(euth_clpm_low,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(income_clpm_low,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(immi_clpm_low,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(eu_clpm_low,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")))

auth_results$lhs <- c("DV: Policy Preference","DV: Personality")
auth_results$rhs <- c("IV: Personality","IV: Policy Preference")
auth_results$label <- c(rep("Ideology",4),rep("Euthanasia",4),rep("Income Differences",4),rep("Immigration",4),rep("EU",4))
auth_results$education <- "low"
auth_results$wave <- c(rep("Wave 2 DV~Wave 1 IV",2),rep("Wave 3 DV~Wave 2 IV",2))


auth_results_high <-
  gtools::smartbind(
    parameterestimates(libcon_clpm_high,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(euth_clpm_high,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(income_clpm_high,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(immi_clpm_high,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(eu_clpm_high,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")))

auth_results_high$lhs <- c("DV: Policy Preference","DV: Personality")
auth_results_high$rhs <- c("IV: Personality","IV: Policy Preference")
auth_results_high$label <- c(rep("Ideology",4),rep("Euthanasia",4),rep("Income Differences",4),rep("Immigration",4),rep("EU",4))
auth_results_high$education <- "high"
auth_results_high$wave <- c(rep("Wave 2 DV~Wave 1 IV",2),rep("Wave 3 DV~Wave 2 IV",2))

comb_graph<- rbind(auth_results, auth_results_high)
comb_graph$lhs <- factor(auth_results$lhs,levels=c("DV: Policy Preference","DV: Personality"))

ggplot(comb_graph,aes(x=label,y=est,shape=wave, colour=education))+geom_pointrange(aes(ymin=ci.lower,ymax=ci.upper),position=position_dodge(width=.5))+facet_wrap(lhs~rhs)+geom_hline(yintercept = 0,lty=2)+theme_bw(base_size=15)+labs(shape="Model")+ylab("Standardized Coefficient")+theme(axis.text.x = element_text(angle = 45,hjust=1))+xlab("Policy Variable") + theme(legend.position="bottom")
ggsave("Results/xlaggedplotliss_by_education.pdf",width=8,height=6)

